//Stephen Erb Application GUI

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;

public class HealthCare extends Person implements ActionListener
{
	private String company;
	private long number;
	private String otherHealth;
	private String location;
        JButton okE;
        JButton closeE;
        JFrame c;
        JFrame n;
        JFrame o;
        JFrame l;
        JTextField companyT;
        JTextField numberT;
        JTextField otherHealthT;
        JTextField locationT;
        public HealthCare() throws Exception
        {       
                company="";
                number=0;
                otherHealth="";
                location="";
                setLayout(new GridLayout(10, 4, 5, 5));
                JLabel companyL= new JLabel("Company: ");
                companyT= new JTextField(25);
                ((AbstractDocument) companyT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
                JLabel numberL= new JLabel("Healthcare Number: ");
                numberT= new JTextField(12);
                ((AbstractDocument) numberT.getDocument()).setDocumentFilter(new EIDFieldDocumentFilter());
                JLabel otherHealthL= new JLabel("Other Healthcare: ");
                otherHealthT= new JTextField(25);
                ((AbstractDocument) otherHealthT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
                JLabel locationL= new JLabel("Location: ");
                locationT= new JTextField(12);
                ((AbstractDocument) locationT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
                okE=new JButton("OK");
                closeE=new JButton("Close");
                okE.setActionCommand("OK");
                closeE.setActionCommand("CLOSE");
                okE.addActionListener(this);
                closeE.addActionListener(this);
                add(companyL);
                add(companyT);
                add(numberL);
                add(numberT);
                add(otherHealthL);
                add(otherHealthT);
                add(locationL);
                add(locationT);
                add(okE);
                add(closeE);
        }
        public boolean validateCompany() {
            boolean valid=true;
            boolean size=true;
            char[] temp=companyT.getText().trim().toCharArray();
            for(int i=0; i<temp.length;i++) {
                if(temp[i]<'A' || temp[i]>'Z')
                {
                    if(temp[i]<'a' || temp[i]>'z')
                    {
                        if(temp[i]!=' ')valid=false;
                    }
                }
            }
            if(temp.length>25 || temp.length==0)size=false;
            if(valid==false || size==false) {
                c=new JFrame("Company Error Message Box");
                c.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close= new JButton("Close");
                close.setActionCommand("COMPANYERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL=new JLabel();
                if(valid==false && size==true) {
                    eL.setText("Only letters and spaces!");   
                }else if(valid==false && size==false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                }else if(valid==true && size==false) {
                    eL.setText("A maximum of twenty-five letters and spaces.");
                }
                c.add(eL);
                c.add(close);
                c.setSize(400,300);
                c.setLocationRelativeTo(null);
                c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                c.setVisible(true);
            }
            if(valid&&size) {
                setCompany(companyT.getText().trim());
            }
            return (valid && size);   
        }
        public boolean validateOtherHealth() {
            boolean valid=true;
            boolean size=true;
            char[] temp=otherHealthT.getText().trim().toCharArray();
            for(int i=0; i<temp.length;i++) {
                if(temp[i]<'A' || temp[i]>'Z')
                {
                    if(temp[i]<'a' || temp[i]>'z')
                    {
                        if(temp[i]!=' ')valid=false;
                    }
                }
            }
            if(temp.length>25 || temp.length==0)size=false;
            if(valid==false || size==false) {
                o=new JFrame("Other Healthcare Error Message Box");
                o.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close= new JButton("Close");
                close.setActionCommand("OTHERHEALTHERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL=new JLabel();
                if(valid==false && size==true) {
                    eL.setText("Only letters and spaces!");   
                }else if(valid==false && size==false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                }else if(valid==true && size==false) {
                    eL.setText("A maximum of twenty-five letters and spaces.");
                }
                o.add(eL);
                o.add(close);
                o.setSize(400,300);
                o.setLocationRelativeTo(null);
                o.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                o.setVisible(true);
            }
            if(valid&&size) {
                setOtherHealth(otherHealthT.getText().trim());
            }
            return (valid && size);   
        }
        public boolean proceedOnward() 
        {
            boolean valid=true;
            if(validateOtherHealth() ==false) valid=false;
            if(validateCompany()==false) valid=false;
            if(validateNumber()==false)valid=false;
            if(validateLocation()==false) valid=false;
            if(validateFName()==false) valid=false;
            if(validateMName()==false)valid=false;
            if(validateLName()==false) valid=false;
            if(validateAge()==false) valid=false;
            if(validateSSN()==false) valid=false;
            return valid;
        }
        public boolean validateNumber() {
            boolean valid=true;
            boolean size=true;
            char[] temp=numberT.getText().toCharArray();
            for(int i=0; i<temp.length;i++) {
                if(temp[i]<'0' || temp[i]>'9')valid=false;
            }
            if(temp.length!=12)size=false;
            if(valid==false || size==false) {
                n=new JFrame("Healthcare Number Error Message");
                n.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close= new JButton("Close");
                close.setActionCommand("HEALTHCARENUMBERERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL=new JLabel();
                if(valid==false && size==true) {
                    eL.setText("Only numbers!");   
                }else if(valid==false && size==false) {
                    eL.setText("Only numbers! Twelve numbers to be exact.");
                }else if(valid==true && size==false) {
                    eL.setText("Only twelve numbers, no more no less than twelve.");
                }
                n.add(eL);
                n.add(close);
                n.setSize(400,300);
                n.setLocationRelativeTo(null);
                n.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                n.setVisible(true);
            }
            if(valid&&size) {
                setNumber(Long.valueOf(numberT.getText()).longValue());
            }
            return (valid && size);
        }
        public void initialize()
        {
                this.setTitle("HealthCare");
                remove(ok);
                remove(close);
                this.setSize(400,300);
                this.setLocationRelativeTo(null);
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setVisible(true);
        }
        public boolean validateLocation() {
            boolean valid=true;
            boolean size=true;
            char[] temp=locationT.getText().trim().toCharArray();
            for(int i=0; i<temp.length;i++) {
                if(temp[i]<'A' || temp[i]>'Z')
                {
                    if(temp[i]<'a' || temp[i]>'z')
                    {
                        if(temp[i]!=' ')valid=false;
                    }
                }
            }
            if(temp.length>25 || temp.length==0)size=false;
            if(valid==false || size==false) {
                l=new JFrame("Location Error Message Box");
                l.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close= new JButton("Close");
                close.setActionCommand("LOCATIONERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL=new JLabel();
                if(valid==false && size==true) {
                    eL.setText("Only letters and spaces!");   
                }else if(valid==false && size==false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                }else if(valid==true && size==false) {
                    eL.setText("A maximum of twenty-five letters and spaces.");
                }
                l.add(eL);
                l.add(close);
                l.setSize(400,300);
                l.setLocationRelativeTo(null);
                l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                l.setVisible(true);
            }
            if(valid&&size) {
                setLocation(locationT.getText().trim());
            }
            return (valid && size);   
        }
        public void actionPerformed(ActionEvent event)
        {
                String command=event.getActionCommand();
                if(command=="OK")
                {
                        if(proceedOnward())
                        {
                            System.out.println(toString());
                            System.exit(0);
                            dispose();
                        }
                }
                else if(command=="CLOSE")
                {
                        dispose();
                        Choose c1=new Choose();
                        c1.initialize();
                }
                else if(command=="OTHERHEALTHERRORCLOSEBOX") 
                {
                        o.dispose();
                }
                else if(command=="HEALTHCARENUMBERERRORCLOSEBOX") 
                {
                        n.dispose();           
                }
                else if(command=="COMPANYERRORCLOSEBOX") 
                {
                        c.dispose();
                }
                else if(command=="LOCATIONERRORCLOSEBOX") 
                {
                        l.dispose();
                }
        }
        public String toString()
        {
                return "First Name: " + getFName() + " Last Name: " + getLName() + " Middle Name: " + getMName() + " Age:" + getAge() + " SSN: " + stringSSN()+"\n"+ "Healthcare: "+ getCompany()+ " Health Number: " + getNumber()+ " Other Healthcare: "+getOtherHealth()+ " Location: "+ getHCLocation();
        }
	public String getCompany()
	{
		return company;
	}
	public long getNumber()
	{
		return number;
	}
	public String getOtherHealth()
	{
		return otherHealth;
	}
	public String getHCLocation()
	{
		return location;
	}
        public void setCompany(String temp)
        {
                company=temp;
        }
        public void setNumber(long temp)
        {
                number=temp;
        }
        public void setOtherHealth(String temp)
        {
                otherHealth=temp;
        }
        public void setLocation(String temp)
        {
                location=temp;
        }
}
